from models import PlanList, Department, VetGroup, Employee, EcoClassModel, WorkPlan, Vetter
from utils.CRUD import crud
from utils.Module import Module
from utils.Vetting import vetting

module_planlist = Module('planlist', url_prefix='/api/aebp')


@crud
@vetting
class PlanListService:
    Model = PlanList

    @classmethod
    async def before_create(cls, request, param):
        if not param['dept_id'] or param['dept_id'] == '':
            raise ValueError('请输入教研室/部门/科室')
        dept_plan_list = await cls.Model.filter(dept_id=param['dept_id']).values()
        dept_plan_list_index = 0
        if len(dept_plan_list):
            dept_plan_list_index = max([item['index'] for item in dept_plan_list])
        param['index'] = dept_plan_list_index + 1
        dept = await Department.filter(id=param['dept_id']).first().values()
        param['code'] = f"PW{dept['pinyin']}{param['index']:02d}"
        return param

    @classmethod
    async def after_create(cls, data):
        return None

    @classmethod
    async def after_select(cls, data):
        user_temp = await Employee.all().values()
        user_list = {}
        for item in user_temp:
            user_list[item['id']] = item['name']

        group_temp = await VetGroup.all().values()
        group_list = {}
        for item in group_temp:
            __member__ = await Vetter.filter(group_id=item['id']).values()
            tag = ""
            for n in __member__:
                if tag == '':
                    tag = user_list[n['expert_id']]
                else:
                    tag += f", {user_list[n['expert_id']]}"
            group_list[item['id']] = tag
        dept_temp = await Department.all().values()
        dept_list = {}
        for item in dept_temp:
            dept_list[item['id']] = item['name']

        eco_class_temp = await EcoClassModel.all().values()
        eco_class_list = {}
        for item in eco_class_temp:
            eco_class_list[str(item['id'])] = item['name']

        for item in data:
            item['dept_name'] = dept_list[item['dept_id']]
            item['requester_name'] = user_list[item['requester']]
            item['eco_class_name'] = eco_class_list[str(item['eco_class_id'])]

            item['applicant_tag'] = group_list[item['applicant_id']] if item['applicant_id'] else None

        return data

    @classmethod
    async def before_update(cls, request, param, data):
        if 'carry_out' in param:
            param['carry_out'] = param['carry_out'] in ['true', 'True', '1', 1]
        return param

    @classmethod
    async def after_update(cls, data):
        return None


module_planlist.router_list([
    ('', PlanListService.Create),
    ('', PlanListService.Select),
    ('/<id:int>', PlanListService.SelectIn),
    ('/approve/<id:str>', PlanListService.Vetting),
    ('/<id:str>', PlanListService.Update),
    ('/<id:str>', PlanListService.Delete),
    ('/<name:str>/<value>', PlanListService.SelectBy)
])
